﻿namespace ScrumFactory.Data.Sql.Tests
{
    using System.Configuration;
    using System.Linq;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using ScrumFactory.Data.Sql;
    using System.Transactions;

    [TestClass()]
    public class SqlProjectRepositoryTest
    {
        [TestMethod()]
        public void GetAllProjects_ReturnsAllProjects()
        {
            var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetAllProjects();

            Assert.IsNotNull(result);
            Assert.AreEqual(4, result.Count);
            Assert.IsTrue(result.Any(p => p.ProjectUId.Trim() == "TEST_PROJECT_A" && p.ProjectName == "Test Project A"));
            Assert.IsTrue(result.Any(p => p.ProjectUId.Trim() == "TEST_PROJECT_B" && p.ProjectName == "Test Project B"));
        }

        [TestMethod()]
        public void GetRunningProjects_ReturnsRunningProjects() {
            var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetRunningProjects();

            Assert.IsNotNull(result);
            Assert.IsTrue(result.All(p => p.Status == (int) ScrumFactory.ProjectStatus.PROJECT_STARTED));
            
        }

        [TestMethod()]
        public void GetProject_ReturnsNull()
        {
            var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);
            var result = target.GetProject("nonExistentProject");
            Assert.IsNull(result);
        }

        [TestMethod()]
        public void GetProject_ReturnsProject()
        {
            var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

            var result = target.GetProject("TEST_PROJECT_A");

            Assert.IsNotNull(result, "Result is null");
            Assert.AreEqual("TEST_PROJECT_A", result.ProjectUId.Trim(), "Id does not match");
            Assert.AreEqual("Test Project A", result.ProjectName, "Name does not match");
        }

        [TestMethod()]
        public void UpdateProject_UpdatesProject()
        {
            using (var transaction = new TransactionScope())
            {
                var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

                target.UpdateProject(new ScrumFactory.Project { ProjectUId = "TEST_PROJECT_A", ProjectName = "New name" });

                var result = target.GetProject("TEST_PROJECT_A");

                Assert.IsNotNull(result, "Result is null");
                Assert.AreEqual("New name", result.ProjectName, "Name does not match");
            }
        }

        [TestMethod()]
        public void InsertProject_InsertsProject()
        {
            using (var transaction = new TransactionScope())
            {
                var target = new SqlProjectRepository(ConfigurationManager.ConnectionStrings["ScrumFactoryEntities"].ConnectionString);

                target.InsertProject(new ScrumFactory.Project { 
                    ProjectUId = "newProject", ProjectName = "New project", ClientName = "ACME", Description="desc", CreateDate = System.DateTime.Now });

                var result = target.GetProject("newProject");

                Assert.IsNotNull(result);
                Assert.AreEqual("newProject", result.ProjectUId.Trim());
                Assert.AreEqual("New project", result.ProjectName);
            }
        }
    }
}
